package com.smallkingson.ylib.core.utils;

import com.smallkingson.ylib.core.Yr;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public  class YFileSelector {
        File root;
        File file;
        List<File> files;

        public static YFileSelector create(String path) {
            YFileSelector fileSelector = new YFileSelector(path);
            fileSelector.refresh();
            return fileSelector;
        }

        private void refresh() {
            if (!file.exists()) {
                throw new IllegalArgumentException("no such dir ：" + file);
            }
            files.clear();
            files.addAll(Arrays.asList(file.listFiles()));
        }

        public void back() {
            if(file.getAbsolutePath().equals(root.getAbsolutePath())){
                return;
            }
            file=file.getParentFile();
            refresh();
        }
        public String getPath(){
            return file.getAbsolutePath();
        }
        public void open(String name) {
            file = new File(file, name);
            refresh();
        }

        private YFileSelector(String path) {
            this.root = new File(path);
            this.file = new File(path);
            files = new ArrayList<File>();
        }

        public List<File> getFileList() {
            return new ArrayList<File>(files);
        }

    public void open(File file) {
        open(file.getName());
    }
}